package org.rapan;

import java.util.Date;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.rapan.EJB.ConsultantInt;
import org.rapan.exceptions.DateMismatchException;
import org.rapan.exceptions.EmailTakenException;
import org.rapan.jpa.Consultant;

import com.vaadin.data.validator.EmailValidator;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;

@SuppressWarnings("serial")
public abstract class ConsultantForm extends Form {
	private TextField nameField = new TextField("Name:");
	private TextField emailField = new TextField("Email:");
	private ComboBox positionBox = new ComboBox("Position:");
	PopupDateField startDate = new PopupDateField("Start Date:");
	PopupDateField endDate = new PopupDateField("End Date:");

	private PasswordField passwordField = new PasswordField("Password :");
	Consultant c;

	public ConsultantForm() {
		super();
		getLayout().setMargin(true);
		nameField.setRequired(true);
		addField("Name", nameField);
		emailField.setRequired(true);
		startDate.setDateFormat("dd-MM-yy");
		endDate.setDateFormat("dd-MM-yy");
		emailField.setImmediate(true);
		emailField.addValidator(new EmailValidator("Invalid email"));
		addField("Email", emailField);
		addField("password", passwordField);
		addField("position", positionBox);
		addField("Start Date", startDate);
		fillComboBox();
	}

	private void fillComboBox() {
		positionBox.setNullSelectionAllowed(false);
		positionBox.addItem(Position.Junior);
		positionBox.addItem(Position.Normal);
		positionBox.addItem(Position.Senior);
		positionBox.setItemCaption(Position.Junior, "Junior consultant");
		positionBox.setItemCaption(Position.Normal, "Consultant");

		positionBox.setItemCaption(Position.Senior, "Senior consultant");
		positionBox.setValue(Position.Junior);

	}

	public ConsultantForm(Consultant c) {
		this();
		this.c = c;
		if (c != null) {
			nameField.setValue(c.getFullName());
			emailField.setValue(c.getEmail());
			positionBox.setValue(c.getPosition());
			startDate.setValue(c.getStartDate());
			addField("endDate", endDate);
			endDate.setValue(c.getEndDate());
		}
	}

	@Override
	public void attach() {
		if (c == null)
			passwordField.setRequired(true);
		Button submit = new Button("Submit");
		submit.addListener(new ClickListener() {

			public void buttonClick(ClickEvent event) {
				post();
			}
		});
		addField("", submit);

	}

	protected void post() {
		if (isValid()) {
			try {

				String email = (emailField.getValue().toString());
				String fullName = (nameField.getValue().toString());
				Position position = ((Position) positionBox.getValue());
				Date startD = (Date) startDate.getValue();

				String password = passwordField.getValue().toString();
				InitialContext context = new InitialContext();
				ConsultantInt conInt = (ConsultantInt) context
						.lookup("Consult/ConsultantBean/local");
				if (c == null) {
					try {
						c = conInt.register(fullName, email, position, startD,
								password);
						Notification not = new Notification(
								"Registration successful. Email sent to consultant.");
						not.setDelayMsec(100 * 20);
						getWindow().showNotification(not);
						EmailUtilities
								.sendSuccessfulConsultantRegistrationEmail(c,
										getApplication().getURL().toString());
						clearForm();
					} catch (EmailTakenException e) {
						getWindow().showNotification(
								"Registration Failed. Email is taken",
								Notification.TYPE_WARNING_MESSAGE);
					}
				} else {
					c.setEmail(email);
					Date endD;

					endD = (Date) endDate.getValue();

					c.setEndDate(endD);
					c.setFullName(fullName);
					if ("".equals(password)) {
						c.setPassword(password);
					}
					c.setStartDate(startD);
					c.setPosition(position);
					try {
						conInt.edit(c);
						clearForm();
						Notification not = new Notification(
								"Editing was successful.");
						not.setDelayMsec(100 * 20);
						getWindow().showNotification(not);
						successfulEdit();

					} catch (DateMismatchException e) {
						getWindow().showNotification(
								"Editing Failed. Date mismatch.",
								Notification.TYPE_WARNING_MESSAGE);
					}

				}
			} catch (NamingException e) {
				e.printStackTrace();
			}

		}
	}

	public abstract void successfulEdit();

	public void clearForm() {
		c = null;
		nameField.setValue("");
		emailField.setValue("");
		startDate.setValue(null);
		endDate.setValue(null);
		passwordField.setValue("");
	}

}
